package auto.test.util;


import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;


public class ExeclUtil {
	
	public static XSSFSheet readexcel() throws Exception {
		GetConfigPath get=new GetConfigPath();
	//    ProUtil ProU=new ProUtil(get.GetconfigPath());
		String file=get.GetelementPath();
		File src = new File(file);
		// 加载文件
		FileInputStream fis = new FileInputStream(src);
		// 加载workbook
		@SuppressWarnings("resource")
		XSSFWorkbook wb=new XSSFWorkbook(fis);
		//加载第一个sheet
	    XSSFSheet sh1= wb.getSheetAt(0);
		//通过sheet名加载sheet
		//XSSFSheet sh1=wb.getSheet();
		return sh1;
	}


	public static XSSFSheet readexcel(String sheetname) throws Exception {
		GetConfigPath get=new GetConfigPath();
	//    ProUtil ProU=new ProUtil(get.GetconfigPath());
		String file=get.GettestNGdataPath();
		File src = new File(file);
		// 加载文件
		FileInputStream fis = new FileInputStream(src);
		// 加载workbook
		@SuppressWarnings("resource")
		XSSFWorkbook wb=new XSSFWorkbook(fis);
		//加载第一个sheet
	    //XSSFSheet sh1= wb.getSheetAt(0);
		XSSFSheet sh1=wb.getSheet(sheetname);

		//通过sheet名加载sheet
		//XSSFSheet sh1=wb.getSheet();
		return sh1;
	}



	public static String getLocatorBy(String key) throws Exception {
		String LocatorBy = null;
		XSSFSheet sh1=readexcel();
		int row=sh1.getPhysicalNumberOfRows();
		for(int i=1;i<row;i++) {
			String value=sh1.getRow(i).getCell(1).getStringCellValue();
			if(value.equals(key)) {
				LocatorBy=sh1.getRow(i).getCell(2).getStringCellValue();
				break;
			}else {
			
			}
			
		}
		return LocatorBy;
	}

	public static String getLocatorValue(String key) throws Exception {
		
		String LocatorValue = null;
		XSSFSheet sh1=readexcel();
		int row=sh1.getPhysicalNumberOfRows();
		for(int i=1;i<row;i++) {
			String value=sh1.getRow(i).getCell(1).getStringCellValue();
			if(value.equals(key)) {
				LocatorValue=sh1.getRow(i).getCell(3).getStringCellValue();
				break;
			}else {
		//	System.out.println("尝试读取中");
			}
			
		}
		return LocatorValue;
	}



	public static String getvalue(String key) throws Exception {
		String LocatorValue = null;
		XSSFSheet sh1=readexcel();
		int row=sh1.getPhysicalNumberOfRows();
		for(int i=1;i<row;i++) {
			String value=sh1.getRow(i).getCell(1).getStringCellValue();
			if(value.equals(key)) {
				LocatorValue=sh1.getRow(i).getCell(3).getStringCellValue();
				break;
			}else {
		//	System.out.println("尝试读取中");
			}
			
		}
		return LocatorValue;
	}
	public static String getvalue(String sheetname,String key) throws Exception {
		String LocatorValue = null;
		XSSFSheet sh1=readexcel(sheetname);
		int row=sh1.getPhysicalNumberOfRows();
		for(int i=1;i<row;i++) {
			String value=sh1.getRow(i).getCell(0).getStringCellValue();
			if(value.equals(key)) {
				LocatorValue=sh1.getRow(i).getCell(1).getStringCellValue();
				break;
			}else {
		//	System.out.println("尝试读取中");
			}

		}
		return LocatorValue;
	}
	/*	// 开始读取第一行第一列的数据
	System.out.println(sh1.getRow(0).getCell(0).getStringCellValue()+"第一行第一列");
	// 读取第一行第二列内容
	System.out.println(sh1.getRow(0).getCell(1).getStringCellValue());
	// 读取第二行第二列内容
	System.out.println(sh1.getRow(1).getCell(0).getStringCellValue());
	// 读取第二行第二列内容
	System.out.println(sh1.getRow(1).getCell(1).getStringCellValue());
	
	// 获取实际总行数
	System.out.println(sh1.getPhysicalNumberOfRows());
	
	// 获取实际总列数
	System.out.println(sh1.getPhysicalNumberOfRows());	
*/

	public static void main(String[] args) throws Exception {
	/*	String LocatorBy=getLocatorBy("Noticemore");
		String LocatorValue=getLocatorValue("Noticemore");
		String Value=getvalue("modify");
	//	System.out.println(System.getProperty("user.dir")+System.getProperty("file.separator")+file); 
		System.out.println(LocatorValue+"\n"+LocatorBy);
		*/
		ExeclUtil execlUtil=new ExeclUtil();

		System.out.println(getvalue("as","password"));

	}

}
 
